package com.wenyu.wenyuyuedu.fragment.circlefragment;


import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.util.RecyclerViewStateUtils;
import com.github.jdsjlzx.view.LoadingFooter;
import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.activity.LoginActivity;
import com.wenyu.wenyuyuedu.adapter.circleAdapter.FindAllDetailAdapter;
import com.wenyu.wenyuyuedu.bean.circle.CircleDetailBean;
import com.wenyu.wenyuyuedu.global.ApiWrapper;
import com.wenyu.wenyuyuedu.server.circle.MyCircleServer;
import com.wenyu.wenyuyuedu.utils.AbSharedUtil;
import com.wenyu.wenyuyuedu.utils.MyToast;
import com.wenyu.wenyuyuedu.utils.SharedConstants;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 * 发现---未加入书圈---的全部的展示
 */
public class FindAll_Invitation_Fragment extends Fragment {

    @InjectView(R.id.default_imageview)
    ImageView defaultImageview;
    private String token;
    private int circleId;
    private int page = 1;
    private LRecyclerView recyclerView;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private List<CircleDetailBean.InfoBeanX.InfoBean> info;
    private FindAllDetailAdapter listAdapter;
    private Intent intent;
    private View view;
    private boolean isRefresh = false;

    public FindAll_Invitation_Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_find_all_invitation_, container, false);
        ButterKnife.inject(this, view);
        initLRecyclerView();
        initView();
        requestData();
        RefreshAndLoadMore();
        return view;
    }

    /*条目的点击事件*/
    private void RefreshAndLoadMore() {
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                MyToast.showToast(getActivity(), "加入后可查看详情", Toast.LENGTH_SHORT);
            }
        });
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                RecyclerViewStateUtils.setFooterViewState(recyclerView, LoadingFooter.State.Normal);
                isRefresh = true;
                MyToast.showToast(getActivity(), "加入书圈后获得最新数据", Toast.LENGTH_SHORT);
                //改变下拉刷新和加载更多的状态
                recyclerView.refreshComplete(page);
            }
        });
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
              /*  if (isRefresh) {
                    return;
                }*/
                isRefresh = false;
                MyToast.showToast(getActivity(), "加入书圈后获得更多数据", Toast.LENGTH_SHORT);
                //改变下拉刷新和加载更多的状态
                recyclerView.refreshComplete(page);
            }
        });
    }

    /*初始化布局*/
    private void initView() {
        intent = getActivity().getIntent();
        String circleIds = intent.getStringExtra("circle_find_id");
        circleId = Integer.parseInt(circleIds);
    }

    /*初始化recyclerview*/
    private void initLRecyclerView() {
        recyclerView = (LRecyclerView) view.findViewById(
                R.id.fragment_list_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);
        listAdapter = new FindAllDetailAdapter(getActivity());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(listAdapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
    }

    /*获取网络数据*/
    private void requestData() {
        token = AbSharedUtil.getString(getActivity(), SharedConstants.USER_TOKEN);
        if (token == null) {
            showLoginDialog();
        } else {
            MyCircleServer myCircleServer = ApiWrapper.getInstance().create(MyCircleServer.class);
            Call<CircleDetailBean> circleDetail = myCircleServer.getCircleDetail(token, circleId, 1, page);
            circleDetail.enqueue(new Callback<CircleDetailBean>() {
                @Override
                public void onResponse(Call<CircleDetailBean> call, Response<CircleDetailBean> response) {
                    CircleDetailBean body = response.body();
                    if (body != null) {
                        int code = body.getCode();
                        if (code == 1) {
                            info = body.getInfo().getInfo();
                            /*创建圈子用户的uid*/
                            String circle_uid = body.getInfo().getCircle_uid();
                            if (info != null) {
                                listAdapter.addAll(info);
                                listAdapter.getUserUid(circle_uid);
                                lRecyclerViewAdapter.notifyDataSetChanged();
                               // RecyclerViewStateUtils.setFooterViewState(recyclerView, LoadingFooter.State.Normal);
                                // circleDetailAdapter.AddAll(info);
                            } else {
                                defaultImageview.setVisibility(View.VISIBLE);
                            }
                        } else if (code == 0) {
                            /*token错误或者已经失效*/
                            showLoginDialog();
                        } else if (code == 3500) {
                                /*需要登陆后才可以操作*/
                        } else if (code == 3501) {
                            /*您必须要先加入书圈之后才可以查看哦！*/

                        } else if (code == 3502) {
                            /*所有的评论已经全部加载完毕！*/
                        } else if (code == 3503) {
                            /*还没有产生神贴*/

                        }
                    }
                }

                @Override
                public void onFailure(Call<CircleDetailBean> call, Throwable t) {
                    MyToast.showToast(getActivity(), "数据异常，请稍后重试", Toast.LENGTH_SHORT);
                }
            });
        }
    }

    AlertDialog ad;
    private TextView quxiao;
    private TextView queding;

    /*token错误或者已失效*/
    private void showLoginDialog() {
        ad = new AlertDialog.Builder(getActivity()).create();
        ad.show();
        Window window = ad.getWindow();
        window.setContentView(R.layout.dailog_login);
        quxiao = (TextView) window.findViewById(R.id.dialog_login_quxiao);
        queding = (TextView) window.findViewById(R.id.dialog_login_queding);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
                getActivity().finish();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                ad.dismiss();
                getActivity().finish();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
